﻿Imports System.Windows.Forms

Public Class frmConfigWRDB

    Private Project As WRDB.Project.clsProject
    Public ProjectName As String
    Public ReportDir As String

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If Not Accept() Then Exit Sub
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Function Accept() As Boolean
        Try
            'see if specified database exists on the server

            If cboWRDBProjects.Text = "" Then Return False

            Project = New WRDB.Project.clsProject(cboWRDBProjects.Text)

            With Project
                If Not .DB.TestConnectionQuick(10) Then
                    ErrorMsg("Unable to connect to database identified by WRDB project: " & cboWRDBProjects.Text, .DB.LastException)
                    Return False
                End If
            End With

            WRDB.Project.clsApplicationSettings.AppDataPath = txtWRDBDir.Text
            ProjectName = cboWRDBProjects.Text
            ReportDir = txtReportDir.Text

            Return True

        Catch ex As Exception
            ErrorMsg(, ex)
            Return False
        End Try

    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnWRDBDir_Click(sender As System.Object, e As System.EventArgs) Handles btnWRDBDir.Click
        Do
            With New System.Windows.Forms.FolderBrowserDialog
                .SelectedPath = txtWRDBDir.Text
                If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    If My.Computer.FileSystem.FileExists(.SelectedPath & "\WRDB6.ini") Then
                        ResetWRDBDir(.SelectedPath)
                        Exit Do
                    Else
                        If MessageBox.Show("This directory does not contain a WRDB configuration file (WRDB6.ini). Please try again or cancel and install WRDB on one computer, specify a network share for the configuration file, and configure the RLIMS project according to the RLIMS Users Guide.", "Warning", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then Exit Sub
                    End If
                End If
                .Dispose()
            End With
        Loop
    End Sub

    Private Sub ResetWRDBDir(NewPath As String)
        txtWRDBDir.Text = NewPath
        WRDB.Project.clsApplicationSettings.AppDataPath = NewPath
        FillWRDBProjectList()
    End Sub

    Private Sub FillWRDBProjectList()
        Me.cboWRDBProjects.Items.Clear()
        For Each s As String In WRDB.Project.clsConnections.GetProjectList.Keys
            Me.cboWRDBProjects.Items.Add(s)
        Next
        If Project IsNot Nothing Then Me.cboWRDBProjects.Text = Project.Name
    End Sub

    Private Sub btnReportDir_Click(sender As System.Object, e As System.EventArgs) Handles btnReportDir.Click
        Do
            With New System.Windows.Forms.FolderBrowserDialog
                .SelectedPath = txtReportDir.Text
                If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    If My.Computer.FileSystem.GetFiles(.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.rdl"}).Count > 0 Then
                        txtReportDir.Text = .SelectedPath
                        Exit Do
                    Else
                        If MessageBox.Show("This directory does not contain any report (*.RDB) files. Please try again or cancel.", "Warning", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then Exit Sub
                    End If
                End If
                .Dispose()
            End With
        Loop

    End Sub

    Private Sub frmConfig_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        txtWRDBDir.Text = WRDB.Project.clsApplicationSettings.AppDataPath
        txtReportDir.Text = ReportDir

        FillWRDBProjectList()

        cboWRDBProjects.Text = ProjectName
    End Sub

    Public Sub New(ProjectName As String, ReportDir As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.ProjectName = ProjectName
        Me.ReportDir = ReportDir

    End Sub

    Private Sub txtReportDir_Validated(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtReportDir.Validated
        If Not My.Computer.FileSystem.DirectoryExists(txtReportDir.Text) OrElse My.Computer.FileSystem.GetFiles(txtWRDBDir.Text & "\WRDB6.ini", FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.rdl"}).Count = 0 Then
            WarningMsg("You must select a folder containing one or more .RDL files.")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtWRDBDir_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtWRDBDir.Validating
        If My.Computer.FileSystem.DirectoryExists(txtWRDBDir.Text) AndAlso My.Computer.FileSystem.FileExists(txtWRDBDir.Text & "\WRDB6.ini") Then
            ResetWRDBDir(txtWRDBDir.Text)
        Else
            WarningMsg("You must select a folder containing WRDB6.ini.")
            e.Cancel = True
        End If
    End Sub
End Class
